//
//  MHChangePasswordViewController.m
//  ManHua
//
//  Created by 江子安 on 15/10/31.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHChangePasswordViewController.h"
#import <AFNetworking.h>

@interface MHChangePasswordViewController ()

@property (weak, nonatomic) IBOutlet UITextField *oldPwd;
@property (weak, nonatomic) IBOutlet UITextField *nowPwd;

@property (weak, nonatomic) IBOutlet UITextField *repeatPwd;


@end

@implementation MHChangePasswordViewController




- (void)changePwd:(id)sender {
    if ([self.nowPwd.text isEqualToString:self.repeatPwd.text]) {
        [self requestDate];
    }
    else
    {
        UIAlertController *failAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次密码输入不同" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        [failAlert addAction:action];
        [self presentViewController:failAlert animated:YES completion:nil];
    }
    
    
}

//http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx?method=changpassword&newpassword=qwerty&userid=138215&oldpassword=123456
- (void)requestDate
{
    NSString *userid = self.userid;
    NSString *pathString = [NSString stringWithFormat:@"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx?method=changpassword&newpassword=%@&userid=%@&oldpassword=%@", self.nowPwd.text, userid, self.oldPwd.text];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:pathString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        if ([result isEqualToString:@"1"]) {
            NSLog(@"修改成功");
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            NSLog(@"修改失败");
            UIAlertController *failAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码修改失败" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
            [failAlert addAction:action];
            [self presentViewController:failAlert animated:YES completion:nil];
        }
    }];
//    NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"changepassword",self.nowPwd,userid,self.oldPwd] forKeys:@[@"method",@"newpassword",@"userid",@"oldpassword"]];
//    [[AFHTTPRequestOperationManager manager] GET:@"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx" parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
//        NSString *result = [NSString stringWithFormat:@"%@", responseObject];
//        if ([result isEqualToString:@"1"]) {
//            [self.navigationController popViewControllerAnimated:YES];
//        }
//        else
//        {
//            NSLog(@"修改失败");
//            UIAlertController *failAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码修改失败" preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
//            [failAlert addAction:action];
//            [self presentViewController:failAlert animated:YES completion:nil];
//        }
//        
//    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
//        NSLog(@"%@",error);
//    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.view = [[[UINib nibWithNibName:@"MHChangePassword" bundle:nil]instantiateWithOwner:self options:nil]lastObject];
    self.navigationItem.title = @"修改密码";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(changePwd:)];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
